#include "aky_com_include.h"

volatile union word_data SensorCount;
volatile union word_data ZeroCount;
volatile union dword_data Math;
volatile union word_data PressuremmHg;
volatile union dword_data TemplateValue;
e_sys_stat_t g_e_sys_stat = {0};
uint16 AverageCount[9];
uint16 PressureBackup;
uint16 g_sec = 0;
uint16 inflat_sec = 0;
uint16 deflat_sec = 0;
volatile uint16_t T1Flag_time_cnt = 0;

void aky_power_init(void)
{
	IP0 = 0x08;			//TC1,2>TC0,T0>INT0>ADC,I2C=UART,SPI=INT1
	IP1 = 0x0c;
#if (AKY_DEBUG_PRINT_TO_SIMUL_UART == 1)
	IP0 = 0x00;			//
	IP1 = 0x00;
	IP0 |= (1 << 2);//tc0设置最高优先级
	IP1 |= (1 << 2);
#endif
	//=== Init Interrup ==========
	EAL=1;
}
void aky_disp_init(void)
{
    //lcd io选择
	P4CON =(0xff);	
	//P3CON &= 0x00;
    LCDM1 &= 0x00;     // Clear LCDM1   
    LCDM1 |= 0x02;     // Enable LCD Pump
		//======== LCD Frame selection ======================	
    // LCDM1 &= 0xF3;     // LCD Clock=256HZ,Frame=64Hz	@4-COM	
    LCDM1 |= 0x08;   // LCD Clock=256HZ,Frame=43Hz	@6-COM	
    //LCDM1 |= 0x04;   // LCD Clock=512HZ,Frame=128Hz	@4-COM	
    //LCDM1 |= 0x0C;   // LCD Clock=512HZ,Frame=85Hz	@6-COM	
 
    LCDM2 &= 0x00;     // Clear LCDM2  
		//======== set Low power mode =======================			
    //LCDM2 &= 0xDF;   // Enable Low power Mode @STOP MODE
    LCDM2 |= 0x20;     // Disable LCD low power mode.

		//======== VLCD  selection ==========================		
    //LCDM2 &= 0xF8;   // VLCD=2.6V
    //LCDM2 |= 0x01;   // VLCD=2.8V
    //LCDM2 |= 0x02;   // VLCD=3.0V
    LCDM2 |= 0x03;     // VLCD=3.2V
    //LCDM2 |= 0x04;   // VLCD=3.4V
    // LCDM2 |= 0x04;   // VLCD=3.6V
	//LCDM2 |= 0x03;   // VLCD=3.2V
		
		//======== Turn on duty  selection @Low Power Mode====	
    // LCDM2 &= 0xE7;     // Set Duty = 4*(1/32k)@Frame=128Hz&85Hz , 8*(1/32k)@Frame=64Hz&43Hz
    LCDM2 |= 0x08;   // Set Duty = 6*(1/32k)@Frame=128Hz&85Hz , 12*(1/32k)@Frame=64Hz&43Hz
    //LCDM2 |= 0x10;   // Set Duty = 8*(1/32k)@Frame=128Hz&85Hz , 16*(1/32k)@Frame=64Hz&43Hz
    //LCDM2 |= 0x18;   // Set Duty = 16*(1/32k)@Frame=128Hz&85Hz , 32*(1/32k)@Frame=64Hz&43Hz

    // LCDM1 |= 0x01;     // Enable LCD 	
	LCDM1 &= 0xfc;

#if (AKY_DEBUG_UART_PRINTF == 1)
    aky_uart_send_string("aky_disp_init\n");
#endif
}

void aky_sys_init(void)
{
    aky_power_init();
    aky_soft_delay_ms(50);
    aky_rcc_init();
    aky_gpio_init();
    aky_disp_init();
    aky_uart_init();

    aky_timer_init(); 
    aky_delay_init();
    // aky_rtc_init(); 
    aky_pwm_init();
    aky_bat_init();
    aky_sdadc_init();
	OLED_Init();
    aky_buzz_init();
    g_s_cal = flash_get_cal_data();

    g_s_set = get_flash_set_data();
}

// 等待所有按键松手
void wait_all_key_release(void)
{
    aky_tick_delay_ms(20);
    while (KEY_POWER == 0 || KEY_MEM == 0 || KEY_SET == 0 )
    {
        AKY_ASM_NOP();
    }
    aky_tick_delay_ms(20);
}

uint16_t key_scan_buf[4];
uint16_t key_release_buf[4];
#define KEY_VALID_TIME (100ul / AKY_TICK_INTERVAL) // MS 有效电平过滤最小时间
#define KEY_RELEASE_TIME (KEY_VALID_TIME / 2) // MS 
void key_scan(void)
{
    static uint8_t flag_key_first_up = 0;
    uint8_t i;
    if (flag_key_first_up == 0) // 初始化
    {
        flag_key_first_up = 1;
        KEY_POWER = 1;
        KEY_SET = 1;
        KEY_MEM = 1;
        for (i = 0; i < sizeof(key_scan_buf) / sizeof(key_scan_buf[0]); i++)
        {
            key_scan_buf[i] = 0;
        }
        for (i = 0; i < sizeof(key_release_buf) / sizeof(key_release_buf[0]); i++)
        {
            key_release_buf[i] = 0;
        }    
    }

    if (KEY_POWERIO == 0)
    {
        key_scan_buf[0]++;
        key_release_buf[0] = 0;
        if (key_scan_buf[0] >= KEY_VALID_TIME)
        {
            key_scan_buf[0] = KEY_VALID_TIME;
            KEY_POWER = 0;
        }
    }
    else
    {
        key_scan_buf[0] = 0;
        key_release_buf[0]++;
        if (key_release_buf[0] >= KEY_RELEASE_TIME)
        {
            key_release_buf[0] = KEY_RELEASE_TIME;
            KEY_POWER = 1;
        }
    }

    if (KEY_SETIO == 0)
    {
        key_scan_buf[1]++;
        key_release_buf[1] = 0;
        if (key_scan_buf[1] >= KEY_VALID_TIME)
        {
            key_scan_buf[1] = KEY_VALID_TIME;
            KEY_SET = 0;
        }
    }
    else
    {
        key_scan_buf[1] = 0;
        key_release_buf[1]++;
        if (key_release_buf[1] >= KEY_RELEASE_TIME)
        {
            key_release_buf[1] = KEY_RELEASE_TIME;
            KEY_SET = 1;
        }
    }

    if (KEY_MEMIO == 0)
    {
        key_scan_buf[2]++;
        key_release_buf[2] = 0;
        if (key_scan_buf[2] >= KEY_VALID_TIME)
        {
            key_scan_buf[2] = KEY_VALID_TIME;
            KEY_MEM = 0;
        }
    }
    else
    {
        key_scan_buf[2] = 0;
        key_release_buf[2]++;
        if (key_release_buf[2] >= KEY_RELEASE_TIME)
        {
            key_release_buf[2] = KEY_RELEASE_TIME;
            KEY_MEM = 1;
        }
    }

    // if (KEY_USERIO == 0)
    // {
    //     key_scan_buf[3]++;
    //     key_release_buf[3] = 0;
    //     if (key_scan_buf[3] >= KEY_VALID_TIME)
    //     {
    //         key_scan_buf[3] = KEY_VALID_TIME;
    //         KEY_USER = 0;
    //     }
    // }
    // else
    // {
    //     key_scan_buf[3] = 0;
    //     key_release_buf[3]++;
    //     if (key_release_buf[3] >= KEY_RELEASE_TIME)
    //     {
    //         key_release_buf[3] = KEY_RELEASE_TIME;
    //         KEY_USER = 1;
    //     }
    // }
}

// 任意按键按下
// return 0 没有按键按下   \1 有按键按下
uint8_t get_any_key_pres(void)
{
    uint8_t res = 0;
    if (KEY_POWER == 0 || KEY_MEM == 0 || KEY_SET == 0)
    {
        res = 1;
    }
    return res;
}
// 带按键检测的延时函数 当有按键按下时则打断延时
void key_exit_delay_ms(uint32_t ms)
{
    uint32_t i = 0;
    for (i = 0; i < (ms / AKY_TICK_INTERVAL); i++)
    {
        aky_tick_delay_ms(AKY_TICK_INTERVAL);
        if (get_any_key_pres() == 1)
        {
            break;
        }
    }
}


void zerotracing(uint8_t flag_unit_icon_on)
{
    unsigned char N;
    ZeroCountw = SensorCountw; // initial sensor count;
    N = 0;
    do
    {
        if (flag_unit_icon_on == 1)
        {

        }
        OLED_ShowString(80, 16, " ",OLED_8X16);
        OLED_Update();

        key_exit_delay_ms(150);
        Mathhw = SensorCountw;
        if (Mathhw > ZeroCountw)
        {
            Mathlw = Mathhw - ZeroCountw;
        }
        else
        {
            Mathlw = ZeroCountw - Mathhw;
        }

        if ((Mathlw > 8) || ((ZeroCountw < 0xffff - ZERO_EXPERIENCE_VALUE) && (ZeroCountw > ZERO_EXPERIENCE_VALUE + 2000)))
        {
            N = 0;
            ZeroCountw = Mathhw;
        }
        OLED_ShowChinese(0, 16, E_NO_INVERSE, "正在校零：");
        OLED_ShowString(80, 16, "0",OLED_8X16);
        OLED_Update();

        // if (mmHgkPaFlag == 1)
        // {
        //     if (UnitSetEnable || UnitIcon)
        //     {
        //             mmHgIconOn;
        //     } // mmHg
        // }
        // else
        // {
        //         kPaIconOn;
        //     kPaPointDIAIconOn;
        // }
        key_exit_delay_ms(150);
        N++;
    } while (N < 5 && KEY_POWER);

    if (KEY_POWER == 0)
    {
        {
            IsPowerOn = 0;
        }
    }
}
// 返回10倍气压值  如300点压力实际返回3000
uint16_t calpressure(e_pres_t e_pres)
{
    uint16_t PressureCountw;
    uint16_t res = 0;
    PressureCountw = SensorCountw - ZeroCountw;
    if (PressureCountw > 30000)
        PressureCountw = 0;
    if (PressureCountw <= g_s_cal.cal_adc_val)
    {
        TemplateValueLong = PressureCountw;
        TemplateValueLong = TemplateValueLong * g_s_cal.B1;
        TemplateValueLong = TemplateValueLong / g_s_cal.cal_adc_val;
        PressuremmHgw = TemplateValueLong;
    }
    else
    {
        TemplateValueLong = PressureCountw - g_s_cal.cal_adc_val;
        TemplateValueLong = TemplateValueLong * g_s_cal.B1;
        TemplateValueLong = TemplateValueLong / g_s_cal.cal_adc_val;
        PressuremmHgw = TemplateValueLong + g_s_cal.B1;
    }
    
    PressureBackup = PressuremmHgw;
    
    if (e_pres == PRES_100_ACCURACY)
    {
        PressuremmHgw /= 10; // 正常mode下,压力精确到个位.校压mode下精确到0.1.
    }
    res = PressuremmHgw;
    
    return res;
}
// 所有while循环和loop循环添加一个跳出条件
// return \1 触发跳出
uint8_t all_while_loop_exit(void)
{
    uint8_t res = 0;
    if (IsPowerOn == 0)
    {
        res = 1;
    }
    return res;
}
void showerror(uint8_t err_code)
{
    PUMP_OFF;
    VALVE_OFF;
    OLED_Clear();
    sprintf(fmt_buf, "ERR%c", err_code);
    OLED_ShowString(50, 0,fmt_buf,OLED_8X16);
    OLED_Update();

    wait_all_key_release();

    off_time_tick_clr();
    while (1)
    {
        if (all_while_loop_exit() == 1)
        {
            break;
        }
        if(aky_off_time_trig(60))
        {
            break;
        }
        key_exit_delay_ms(30);
        // if (aky_off_time_trig(g_s_func.off_time))
        // {
        //     IsPowerOn = 0;
        //     break;
        // }

        if (get_any_key_pres() == 1)
        {
            break;
        }
    }

    OLED_Clear(); /* dark all pattern on LCD */
    OLED_Update();
}


void auto_cal_mode(void)
{
    g_s_cal = flash_get_cal_data();

	OLED_Clear();
    olde_fmt_num(0, 16,OLED_8X16,"cal_cnt=",g_s_cal.cal_cnt);
    OLED_Update();
    aky_tick_delay_ms(1000);

	OLED_Clear();

    wait_all_key_release();
    PUMP_OFF;
    VALVE_ON;
    zerotracing(0);
    wait_all_key_release();
    while (1)
    {
        calpressure(PRES_1000_ACCURACY);
        OLED_Clear();
    	OLED_ShowChinese(16, 0,E_NO_INVERSE, "实时压力值：");
        sprintf(fmt_buf, "%03d:%d0", PressuremmHgw / 10,PressuremmHgw%10);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);
        OLED_ShowString(88,48,"mmHg",OLED_8X16);
        olde_fmt_num(0, 56,OLED_6X8,"",SensorCountw);

        OLED_Update();

        key_exit_delay_ms(50);
        if (KEY_POWER == 0)
        {
            break;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
            /* code */
        }
        
    }
    wait_all_key_release();
    g_s_cal.B1 = 3000;
    g_s_cal.cal_adc_val = (SensorCountw - ZeroCountw) / SpanValve;
    calpressure(PRES_1000_ACCURACY);

    if (g_s_cal.cal_adc_val <= AA1_EXPERIENCE_VALUE - AA1_EXPORIENCE_VALUE_ERROR_RANGE || g_s_cal.cal_adc_val >= AA1_EXPERIENCE_VALUE + AA1_EXPORIENCE_VALUE_ERROR_RANGE)
    {
        goto calerror;
    }
    else
    {
    }
    OLED_Clear();
    flash_save_cal_data(g_s_cal);
    g_s_cal = flash_get_cal_data();

    sprintf(fmt_buf, "%03d:%d", PressuremmHgw / 10,PressuremmHgw%10);
    OLED_ShowString(16,16,fmt_buf,OLED_16X32);
    OLED_ShowString(88,48,"mmHg",OLED_8X16);
    olde_fmt_num(0, 56,OLED_6X8,"",g_s_cal.cal_adc_val);

    OLED_Update();

    aky_tick_delay_ms(2000);
    goto CalOverOut;
calerror:
    showerror('C');
    goto CalOverOut;

CalOverOut:
    OLED_Clear();
    OLED_Update();

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}


void linear_mode(void) // 线性模式
{
    uint8_t i = 0;
    LCD_BL_ON;
    g_s_cal = flash_get_cal_data();
    OLED_Clear();
    OLED_ShowString(30, 0, "C-- MODE", OLED_8X16);

    olde_fmt_num(0, 16,OLED_8X16,"ver_month=",ver_month);
    olde_fmt_num(0, 32,OLED_8X16,"ver_day=",ver_day);
    OLED_Update();

    aky_tick_delay_ms(1000);
    if (KEY_SET == 0)
    {
        off_time_tick_clr();
        while (KEY_SET == 0)
        {
            if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 2)
            {
                //axd长按跳转校压
                g_e_sys_stat = CAL_MODE;
                return;
            }
        }
    }
    OLED_Clear();
    OLED_ShowString(30, 0, "C-- MODE", OLED_8X16);

    olde_fmt_num(0, 16,OLED_8X16,"B1=",g_s_cal.B1);
    olde_fmt_num(0, 32,OLED_8X16,"cal_adc=",g_s_cal.cal_adc_val);
    OLED_Update();

    aky_tick_delay_ms(1000);

    OLED_Clear();
    OLED_ShowString(30, 0, "C-- MODE", OLED_8X16);
    OLED_Update();

    wait_all_key_release();

    VALVE_ON;
    PUMP_OFF;
    zerotracing(0);
    wait_all_key_release();
    while (1)
    {
        calpressure(PRES_1000_ACCURACY);
        OLED_Clear();
        OLED_ShowString(30, 0, "C-- MODE", OLED_8X16);
        sprintf(fmt_buf, "%03d:%d0", PressuremmHgw / 10,PressuremmHgw%10);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);
        OLED_ShowString(88,48,"mmHg",OLED_8X16);
        olde_fmt_num(0, 56,OLED_6X8,"",SensorCountw);

        OLED_Update();

        if (PressuremmHgw > 3250)
        {
            //线性模式超325mmgh报错er3
            showerror('3');
            goto linear_mode_exit;
        }
        
        key_exit_delay_ms(50);
        if (KEY_POWER == 0)
        {
            // off_time_tick_clr();
            // while (KEY_POWER == 0)
            // {
            //     if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 2)
            //     {

            //         break;
            //     }
            // }
            aky_tick_delay_ms(50);
            break;
        }

        if (KEY_MEM == 0)
        {
            // off_time_tick_clr();
            // while (KEY_MEM == 0)
            // {
            //     if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 3)
            //     {
            //         flag_emc_mode = 1;
            //         IsPowerOn = 1;
            //         g_e_sys_stat = START_TEST_MODE;
            //         return;
            //     }
            // }
            // life_test_mode();
            // aky_tick_delay_ms(50);
            goto linear_mode_exit;
        }

        if (KEY_SET == 0)
        {
            // off_time_tick_clr();
            // while (KEY_SET == 0)
            // {
            //     if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 3)
            //     {
            //         write_full_mem_bank(g_s_set.e_user);
            //         goto linear_mode_exit;
            //     }
            // }
            // aky_tick_delay_ms(50);
            goto linear_mode_exit;
        }
    }

linear_mode_exit:
    OLED_Clear();
    OLED_Update();

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}



void first_up_mode(void)
{
    LCD_BL_ON;
    OLED_Clear();
    OLED_ShowChinese(16,0,E_NO_INVERSE,"气密测试治具");

    OLED_Update();
    long_beep();
    off_time_tick_clr();
    while (get_cur_off_time_tick() < OFF_TIME_TICK_100MS_COUNT * 10)
    {
        if(uart_rx_trig())
        {
            TUDetect();
            IsPowerOn = 0;
            g_e_sys_stat = STOP_MODE;
            return;
        }
    }

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}

// 开启key中断唤醒
void key_wakeup_enable(void)
{
    P1W	 =  0x0c;		//01011100		// 1 is wakeup pin
    // if (g_s_func.e_power_type != DRY_BAT)
    {
        //只有锂电情况下才打开full charge 引脚唤醒
        P1W	 |=  0x50;		//01011100		// 1 is wakeup pin
    }

    PEDGE &= 0x00; // Clear PEDGE 
    PEDGE |= 0x02; // EX0G = 0x10 : INT0 Falling edge trigger (default). 
    EX0 = 1; // INT0 isr enable
    PEDGE |= 0x04; // EX0G = 0x10 : INT0 Falling edge trigger (default).
    EX1 = 1; // INT1 isr enable
    EAL = 1; // Interrupt enable
}

// 关闭key中断唤醒
void key_wakeup_disable(void)
{
    EX0 = 0;
    EX1 = 0;
}

void stop_mode(void)
{
	BB=POWER | VALVE | PUMP;		
	iocontroloff();  

    PressuremmHgw = 0;
    memset((uint8_t *)&g_s_flag_group, 0x00, sizeof(s_flag_group_t)); // 清空flag
    if (get_cur_charge_stat() == NO_CHARGE)
    {
        LCD_BL_OFF;
        LCD_POWER_OFF;
    }
    else
    {
        IsPowerOn = 1;
        g_e_sys_stat = CHARGE_MODE;
        return;
    }
halt:
    VALVE_OFF;
    PUMP_OFF;
    aky_gpio_enter_stop_mode();                                       // io
    wait_all_key_release();

	TC2Disable;
	TC0M=0;
	RXDisable;
	RXIDisable;	
    BUZZER_DIS;
    
	LCDM1 &= 0xfc;
	LCDM2&=0xDF;
	LBTM&=0xFE;						//close Comparator
	LBTM&=0x1f;						//p1.0,p1.1 set to GPIO;
	OPM	&=0xfe;						//OPA disable;
	S0CON	&=0xcf;					//uart S0CON.4 .5=0;
	VREG	=0x00;					//disable chargepump;
	CHS	 = 0x10;
	AMPM 	= 0x00;
	ADCM1	=0x06;						//close ADC	


    key_wakeup_enable();

   auto_test_stop_mode();

loop:
    SCStopMode; // 进入STOP低功耗模式
    AKY_ASM_NOP();
    AKY_ASM_NOP();
    AKY_ASM_NOP();
    AKY_ASM_NOP();
 
    if (KEY_POWERIO == 0 || KEY_SETIO == 0 || KEY_MEMIO == 0)
    {
        goto wake;
    }
    else if (FULL == 0 || CHARGE == 0)
    {
        goto wake;
    }
#if (AKY_DEBUG_NO_UATOTEST_ENABLE == 0)
    if(SIMUL_RX == 0)
    {
        AKYAutoTestFlag = 1;
        goto wake;
    }
#endif


goto loop;

wake:
    if (KEY_POWERIO == 0)
    {
        KEY_POWER = 0;
    }
    if (KEY_SETIO == 0)
    {
        KEY_SET = 0;
    }
    if (KEY_MEMIO == 0)
    {
        KEY_MEM = 0;
    }
    auto_test_deinit();
    aky_power_init();
    aky_gpio_init();
    aky_disp_init();
    aky_uart_init();

    aky_timer_init(); 
    aky_delay_init();
    aky_pwm_init();
    aky_bat_init();
    aky_sdadc_init();

    g_s_cal = flash_get_cal_data();

    
    if (KEY_POWER == 0)
    {
        g_e_sys_stat = MENU_SEL_MODE;
    }

    if ((FULL == 0 || CHARGE == 0)) // 锂电充电
    {
        g_e_sys_stat = CHARGE_MODE;
    }

    if (g_e_sys_stat == STOP_MODE)
    {
        if(AKYAutoTestFlag == 1)
        {
            AKYAutoTestFlag = 0;
            auto_test_init();
        	OLED_Init();
            TUDetect();
        }
        goto halt;
    }
    
    LCD_POWER_ON;
	OLED_Init();
    IsPowerOn = 1;
    off_time_tick_clr();
}
const uint8_t * p_menu_name_buf[] = 
{
    "测漏速模式",
    "测气密模式",
    "测充泄气模式",
    "压力表模式",
};
void menu_sel_mode(void)
{
    uint8_t i = 0;
    uint8_t buf_size = 0;
    uint8_t selec_index = 0;
    uint8_t key_mem_cnt = 0;
	OLED_Clear();
    OLED_Update();

    PUMP_OFF;
    VALVE_OFF;
    PressuremmHgw = 0;

    if (check_low_bat_pro(LOW_BAT_VAL) == 1)
    {
        g_e_sys_stat = LOW_BAT_MODE;
        return;
    }

    if (KEY_POWER == 0)
    {
        off_time_tick_clr();
        while (KEY_POWER == 0)
        {
            if (get_cur_off_time_tick() > 3000 / AKY_TICK_INTERVAL)
            {
                off_time_tick_clr();
                IsPowerOn = 1;
                g_e_sys_stat = SET_MODE;
                return;
                /* code */
            }
            if (KEY_MEM == 0)
            {
                key_mem_cnt++;
                while (KEY_MEM == 0)
                {
                    /* code */
                }
                
            }
            if (KEY_SET == 0)
            {
                if (key_mem_cnt >= 2)
                {
                    IsPowerOn = 1;
                    g_e_sys_stat = LINEAR_MODE;
                    return;
                }
            }
            
            /* code */
        }
        
        /* code */
    }
    g_s_set = get_flash_set_data();
    selec_index = g_s_set.last_selec_menu_index;

	OLED_Clear();
	OLED_DrawRectangle(0, selec_index * 16, 127, 16, OLED_FILLED);
    buf_size = sizeof(p_menu_name_buf) / sizeof(p_menu_name_buf[0]);
    for (i = 0; i < buf_size; i++)
    {
        if (i == selec_index)
        {
        	OLED_ShowChinese(0, i * 16,E_INVERSE, p_menu_name_buf[i]);
        }
        else
        {
        	OLED_ShowChinese(0, i * 16,E_NO_INVERSE, p_menu_name_buf[i]);
        }
    }

    OLED_Update();
    LCD_BL_ON;

    wait_all_key_release();
    off_time_tick_clr();
    while (1)
    {
        key_exit_delay_ms(100);

        if (KEY_SET == 0)
        {
            selec_index++;
            if (selec_index > 3)
            {
                selec_index = 0;
            }
            OLED_Clear();
            OLED_DrawRectangle(0, selec_index * 16, 127, 16, OLED_FILLED);
            buf_size = sizeof(p_menu_name_buf) / sizeof(p_menu_name_buf[0]);
            for (i = 0; i < buf_size; i++)
            {
                if (i == selec_index)
                {
                	OLED_ShowChinese(0, i * 16,E_INVERSE, p_menu_name_buf[i]);
                }
                else
                {
                	OLED_ShowChinese(0, i * 16,E_NO_INVERSE, p_menu_name_buf[i]);
                }
            }

            OLED_Update();

            off_time_tick_clr();
            while (KEY_SET == 0)
            {
                /* code */
            }
        }
        if (KEY_MEM == 0)
        {
            if (selec_index == 0)
            {
                g_e_sys_stat = LEAK_TEST_MODE;
            }
            else if (selec_index == 1)
            {
                g_e_sys_stat = AIR_TIGHT_TEST_MODE;
            }
            else if (selec_index == 2)
            {
                g_e_sys_stat = INFLAT_DEFLAT_TIME_TEST_MODE;
            }            
            else if (selec_index == 3)
            {
                g_e_sys_stat = PRES_METER_MODE;
            }        

            if (g_s_set.last_selec_menu_index != selec_index)
            {
                g_s_set.last_selec_menu_index = selec_index;
                store_set_to_flash(g_s_set);
            } 
            IsPowerOn = 1;
            return;
        }
        

        if(aky_off_time_trig(OFF_TIME))
        {
            break;
        }
        if (KEY_POWER == 0)
        {
            break;
        }
        
    }

//MENU_SEL_EXIT:
    OLED_Clear();
    OLED_Update();

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}


void set_mode(void)
{
    g_s_set = get_flash_set_data();

    LCD_BL_ON;
	OLED_Clear();
	OLED_ShowChinese(0, 0, E_NO_INVERSE,"充泄气限定压力值：");
    sprintf(fmt_buf, "%d mmHg",g_s_set.inflat_limit_pres_val);
    OLED_ShowString(0,16,fmt_buf,OLED_8X16);

    OLED_Update();

    wait_all_key_release();
    off_time_tick_clr();
    while (1)
    {
        key_exit_delay_ms(100);

        if (KEY_SET == 0)
        {
            off_time_tick_clr();
ADD_INFLAT_PRES:
            g_s_set.inflat_limit_pres_val+=10;
            if (g_s_set.inflat_limit_pres_val > 300)
            {
                g_s_set.inflat_limit_pres_val = 220;
            }
        	OLED_Clear();
        	OLED_ShowChinese(0, 0,E_NO_INVERSE, "充泄气限定压力值：");
            sprintf(fmt_buf, "%d mmHg",g_s_set.inflat_limit_pres_val);
            OLED_ShowString(0,16,fmt_buf,OLED_8X16);

            OLED_Update();

            while (KEY_SET == 0)
            {
                if (get_cur_off_time_tick() > 2000 / AKY_TICK_INTERVAL)
                {
                    aky_tick_delay_ms(200);
                    goto ADD_INFLAT_PRES;
                }
                
            }
        }
        
        if (aky_off_time_trig(OFF_TIME) == 1)
        {
            goto SET_EXIT;
        }
        
        if (KEY_POWER == 0)
        {
            goto SET_EXIT;
        }
        if (KEY_MEM == 0)
        {
            break;
        }
        
    }

// 	OLED_Clear();
// 	OLED_ShowChinese(0, 0, E_NO_INVERSE,"充泄气充气时间：");
//     sprintf(fmt_buf, "%d S",g_s_set.inflat_time);
//     OLED_ShowString(0,16,fmt_buf,OLED_8X16);

//     OLED_Update();

//     wait_all_key_release();
//     off_time_tick_clr();
//     while (1)
//     {
//         key_exit_delay_ms(100);

//         if (KEY_SET == 0)
//         {
//             off_time_tick_clr();
// ADD_INFLAT_TIME:
//             g_s_set.inflat_time+=1;
//             if (g_s_set.inflat_time > 30)
//             {
//                 g_s_set.inflat_time = 10;
//             }
//         	OLED_Clear();
//         	OLED_ShowChinese(0, 0,E_NO_INVERSE, "充泄气充气时间：");
//             sprintf(fmt_buf, "%d S",g_s_set.inflat_time);
//             OLED_ShowString(0,16,fmt_buf,OLED_8X16);

//             OLED_Update();

//             while (KEY_SET == 0)
//             {
//                 if (get_cur_off_time_tick() > 2000 / AKY_TICK_INTERVAL)
//                 {
//                     aky_tick_delay_ms(200);
//                     goto ADD_INFLAT_TIME;
//                 }
                
//             }
//         }
        
//         if (aky_off_time_trig(OFF_TIME) == 1)
//         {
//             goto SET_EXIT;
//         }
        
//         if (KEY_POWER == 0)
//         {
//             goto SET_EXIT;
//         }
//         if (KEY_MEM == 0)
//         {
//             break;
//         }
        
//     }

// 	OLED_Clear();
// 	OLED_ShowChinese(0, 0, E_NO_INVERSE,"充泄气泄气时间：");
//     sprintf(fmt_buf, "%d S",g_s_set.deflat_time);
//     OLED_ShowString(0,16,fmt_buf,OLED_8X16);

//     OLED_Update();

//     wait_all_key_release();
//     off_time_tick_clr();
//     while (1)
//     {
//         key_exit_delay_ms(100);
//         if (KEY_SET == 0)
//         {
//             off_time_tick_clr();
// SUB_DEFLAT_TIME:
//             if (g_s_set.deflat_time <= 2)
//             {
//                 g_s_set.deflat_time = 10;
//             }
//             else
//             {
//                 g_s_set.deflat_time -= 1;
//             }
            
//         	OLED_Clear();
//         	OLED_ShowChinese(0, 0,E_NO_INVERSE, "充泄气泄气时间：");
//             sprintf(fmt_buf, "%d S",g_s_set.deflat_time);
//             OLED_ShowString(0,16,fmt_buf,OLED_8X16);

//             OLED_Update();

//             while (KEY_SET == 0)
//             {
//                 if (get_cur_off_time_tick() > 2000 / AKY_TICK_INTERVAL)
//                 {
//                     aky_tick_delay_ms(200);
//                     goto SUB_DEFLAT_TIME;
//                 }
                
//             }
//         }
        
//         if (aky_off_time_trig(OFF_TIME) == 1)
//         {
//             goto SET_EXIT;
//         }
        
//         if (KEY_POWER == 0)
//         {
//             goto SET_EXIT;
//         }
//         if (KEY_MEM == 0)
//         {
//             break;
//         }
        
//     }


SET_EXIT:
    store_set_to_flash(g_s_set);

    OLED_Clear();
    OLED_Update();

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}


// 低电压模式
void low_bat_mode(void)
{
    IsPowerOn = 1;
    VALVE_OFF;
    PUMP_OFF;

    LCD_BL_ON;
    OLED_Clear();
    OLED_ShowChinese(16, 0, E_NO_INVERSE,"电池电量不足");
    low_bat_icon_on();
    OLED_Update();

    wait_all_key_release();
    beep_times(3);
    off_time_tick_clr();
    while (1)
    {
        if (all_while_loop_exit() == 1)
        {
            break;
        }
        if (aky_off_time_trig(5) == 1)
        {
            break;
        }

        if (KEY_POWER == 0)
        {
            break;
        }
    }

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}
// 充电模式
void charge_mode(void)
{
    uint16_t bat_val;
    uint16_t bat_cells;
    uint16_t show_cells;

    IsPowerOn = 1;
    VALVE_OFF;
    PUMP_OFF;

    LCD_BL_ON;
    
    aky_adc_bat_init();
    aky_tick_delay_ms(50);
    bat_val = get_cur_bat_val_by_math();
    bat_cells = get_baterry_cells(bat_val);

    OLED_Clear();
    // OLED_ShowChinese(16, 0, E_NO_INVERSE,"正在充电");
    show_baterry_cells(bat_cells);//快速刷新显示一次
    OLED_Update();


    wait_all_key_release();
    
    off_time_tick_clr();
    while (1)
    {
        if(uart_rx_trig())
        {
            //锂电usb插入充电时检测自动工装
            TUDetect();
        }
        if (TIME_320MS == 1)
        {
            if (g_s_flag_group.flag_one_shot == 1)
            {
                g_s_flag_group.flag_one_shot = 0;
                if (get_cur_charge_stat() == FULL_BAT)
                {
                    //充满电关闭背光
                    if (get_cur_off_time_tick() > 3000 / AKY_TICK_INTERVAL)
                    {
                        //3s
                        LCD_BL_OFF;
                    }
                }
                //充电3s关闭背光
                if (get_cur_off_time_tick() > 3000 / AKY_TICK_INTERVAL)
                {
                    //3s
                    LCD_BL_OFF;
                }
                
                bat_val = get_cur_bat_val_by_math();
                bat_cells = get_baterry_cells(bat_val);
#if (AKY_DEBUG_UART_PRINTF == 1)
    aky_uart_fmt_num("bat_val = ",get_cur_bat_val_by_math());
    aky_uart_fmt_num("bat_cells = ",get_baterry_cells(bat_val));
    aky_uart_fmt_num("get_cur_charge_stat = ",get_cur_charge_stat());
#endif
                if (get_cur_charge_stat() == FULL_BAT)
                {
                    //充满显示满格
                    show_cells = 3;
                }
                else
                {
                    show_cells++;
                    if (show_cells > 3)
                    {
                        //最大格数则从当前格数重新开始跑马
                        if (bat_cells == 3)
                        {
                            show_cells = 2;
                        }
                        else
                        {
                            show_cells = bat_cells;
                        }
                    }
                }
                OLED_Clear();
                // OLED_ShowChinese(16, 0, E_NO_INVERSE,"正在充电");
                show_baterry_cells(show_cells);//快速刷新显示一次
                OLED_Update();
            }
        }
        else
        {
            g_s_flag_group.flag_one_shot = 1;
        }

        if (all_while_loop_exit() == 1)
        {
            break;
        }
        if (get_cur_charge_stat() == NO_CHARGE)
        {
            break;
        }

        if (KEY_POWER == 0)
        {
            break;
        }
    }
    aky_sdadc_init();

    if (KEY_POWER == 0)
    {
        IsPowerOn = 1;
        g_e_sys_stat = MENU_SEL_MODE;
        return;
    }

    OLED_Clear();
    OLED_Update();
    off_time_tick_clr();
    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;

}

void timer_base_pro(void)
{
    static uint16_t time_cnt = 0;
    static uint16_t time_cnt3 = 0;
    flag_timer_10ms = 1;
    time_cnt++;
    if (time_cnt >= 125 / AKY_TICK_INTERVAL)
    {
        time_cnt = 0;
        T125Flag = 1;
    }
    T1Flag_time_cnt++;
    if (T1Flag_time_cnt >= 1000 / AKY_TICK_INTERVAL)
    {
        T1Flag_time_cnt = 0;
        T1Flag = 1;
        g_sec++;
    }
    time_cnt3++;
    if (time_cnt3 >= 20 / AKY_TICK_INTERVAL)
    {
        time_cnt3 = 0;
    }
}
void LeakTest(void)		//	漏速测试
{
    uint8_t i;
	uint16 temp;
	uint16	L1,L2,L3,L4;
 	uint8	flag_L1_get_onece,flag_L2_get_onece,flag_L3_get_onece,flag_L4_get_onece;
   LCD_BL_ON;

LEAK_TEST_RESTART:
    VALVE_OFF;
    PUMP_OFF;

	L1=L2=L3=L4=199;
    flag_L1_get_onece  = 0;
    flag_L2_get_onece  = 0;
    flag_L3_get_onece  = 0;
    flag_L4_get_onece  = 0;

    OLED_Clear();
    // OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
    // OLED_ShowString(16,16,"0",OLED_16X32);
    // OLED_ShowString(80,32,"mmHg",OLED_8X16);
    OLED_Update();
    
	wait_all_key_release();
    OLED_Clear();

    zerotracing(0);
    if (IsPowerOn == 0)
    {
        goto LEAK_TEST_EXIT;
    }
    
    wait_all_key_release();

    VALVE_ON;
    PUMP_ON;
    off_time_tick_clr();
    while (1)
    {
        calpressure(PRES_100_ACCURACY);
        if (PressuremmHgw >= 300)
        {
            PUMP_OFF;
            break;
        }
        
        OLED_Clear();
        OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
        sprintf(fmt_buf,"%d",PressuremmHgw);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);
        OLED_ShowString(80,32,"mmHg",OLED_8X16);
        OLED_Update();
        key_exit_delay_ms(50);

        if (KEY_POWER == 0)
        {
            goto LEAK_TEST_EXIT;
        }
        if (KEY_MEM == 0)
        {
            goto LEAK_TEST_RESTART;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }

        if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 60)
        {
            showerror('3');
            if (get_any_key_pres() == 0)
            {
                goto LEAK_TEST_EXIT;
            }
        }
        
    }

    OLED_Clear();
    OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
    sprintf(fmt_buf,"%d",PressuremmHgw);
    OLED_ShowString(16,16,fmt_buf,OLED_16X32);
    OLED_ShowString(80,32,"mmHg",OLED_8X16);
    OLED_Update();
    off_time_tick_clr();
    while (1)
    {
        if(flag_adc_ok == 1)
        {
            flag_adc_ok = 0;
            calpressure(PRES_100_ACCURACY);

            OLED_ClearArea(0, 16, 80, 32);
            sprintf(fmt_buf,"%d",PressuremmHgw);
            OLED_ShowString(16,16,fmt_buf,OLED_16X32);
            if (T125Flag == 1)
            {
                T125Flag = 0;
        		for(i=0;i<8;i++)
        		{
            		AverageCount[i]=AverageCount[i+1];
        		}
            	AverageCount[8]=PressureBackup;
            }

        	if(T1Flag==1)
    		{
                //1s
        		T1Flag=0;
        		temp=AverageCount[0]-AverageCount[8];
                //1s最旧的 - 最新的
                if (temp > 300)
                {
                    temp = 300;
                }
                #if (AKY_DEBUG_LCD_PRINT_ENABLE == 1)
                OLED_ClearArea(0, 56, 90, 8);
                sprintf(fmt_buf,"1s def=%d.%d",(temp/10),(temp%10));
                OLED_ShowString(0,56,fmt_buf,OLED_6X8);

                #endif
        	}
            OLED_Update();

        	if(PressuremmHgw==180&&flag_L1_get_onece == 0)
        	{
                //记住达到180时的1s泄气值
                flag_L1_get_onece = 1;
        		L1=AverageCount[0]-AverageCount[8];
        	} 
        	if(PressuremmHgw==120&& flag_L2_get_onece == 0)
        	{
                //记住达到120时的1s泄气值
                flag_L2_get_onece = 1;
        		L2=AverageCount[0]-AverageCount[8];
        	} 
        	if(PressuremmHgw==80&& flag_L3_get_onece == 0)
        	{
                //记住达到80时的1s泄气值
                flag_L3_get_onece = 1;
        		L3=AverageCount[0]-AverageCount[8];
        	} 
        	if(PressuremmHgw<=60)
        	{
        		L4=AverageCount[0]-AverageCount[8];
                VALVE_OFF;
                break;
        	}		

            if (KEY_POWER == 0)
            {
                goto LEAK_TEST_EXIT;
            }
            if (KEY_MEM == 0)
            {
                goto LEAK_TEST_RESTART;
            }
            if (KEY_SET == 0)
            {
                IsPowerOn = 1;
                g_e_sys_stat = MENU_SEL_MODE;
                return;
            }
            if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 240)
            {
                showerror('3');
                if (get_any_key_pres() == 0)
                {
                    goto LEAK_TEST_EXIT;
                }
            }

        }
    }

    OLED_Clear();
    OLED_ShowChinese(0, 0,E_NO_INVERSE, "漏气速率结果：");
    sprintf(fmt_buf,"%d.%d ~ %d.%d",L3/10,L3%10,L2/10,L2%10);
    OLED_ShowString(0,16,fmt_buf,OLED_8X16);
    OLED_ShowString(80,32,"mmHg/s",OLED_8X16);

    #if (AKY_DEBUG_LCD_PRINT_ENABLE == 1)
    sprintf(fmt_buf,"L1=%dL2=%dL3=%dL4=%d",L1,L2,L3,L4);
    OLED_ShowString(0,56,fmt_buf,OLED_6X8);
    #endif
    OLED_Update();

    VALVE_OFF;
    PUMP_OFF;

    off_time_tick_clr();
    while (1)
    {
        if (KEY_POWER == 0)
        {
            goto LEAK_TEST_EXIT;
        }
        if (KEY_MEM == 0)
        {
            goto LEAK_TEST_RESTART;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }
        if (aky_off_time_trig(OFF_TIME))
        {
            goto LEAK_TEST_EXIT;
        }
        
    }

LEAK_TEST_EXIT:
    AKY_ASM_NOP();
    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}

#define AIR_TEST_PRES (280ul)
#define AIR_TEST_TIME (120ul)

void air_tight_test(void)		//
{
	uint16 OneSecDeflated;
	uint16	L1,L2;
	uint16 PressureBackup_last = 0;
	uint16	temp;
    uint8_t i;

    LCD_BL_ON;

AIR_TIGHT_RESTART:
    VALVE_OFF;
    PUMP_OFF;

	L1=L2=199;
    OLED_Clear();
    // OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
    // OLED_ShowString(16,16,"0",OLED_16X32);
    // OLED_ShowString(80,32,"mmHg",OLED_8X16);
    OLED_Update();
    
	wait_all_key_release();
    OLED_Clear();

    zerotracing(0);
    if (IsPowerOn == 0)
    {
        goto AIR_TIGHT_EXIT;
    }
    
    wait_all_key_release();

    VALVE_ON;
    PUMP_ON;
    off_time_tick_clr();
    while (1)
    {
        calpressure(PRES_100_ACCURACY);
        if (PressuremmHgw >= AIR_TEST_PRES)
        {
            PUMP_OFF;
            break;
        }
        OLED_Clear();
        OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
        sprintf(fmt_buf,"%d",PressuremmHgw);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);
        OLED_ShowString(80,32,"mmHg",OLED_8X16);
        OLED_Update();
        key_exit_delay_ms(50);

        if (KEY_POWER == 0)
        {
            goto AIR_TIGHT_EXIT;
        }
        if (KEY_MEM == 0)
        {
            goto AIR_TIGHT_RESTART;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }
        if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 60)
        {
            showerror('3');
            if (get_any_key_pres() == 0)
            {
                goto AIR_TIGHT_EXIT;
            }
        }
        
    }

    OLED_Clear();
    OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
    sprintf(fmt_buf,"%d",PressuremmHgw);
    OLED_ShowString(16,16,fmt_buf,OLED_16X32);
    OLED_ShowString(80,32,"mmHg",OLED_8X16);
    OLED_Update();
    off_time_tick_clr();
    g_sec = 0;
    while (1)
    {
        calpressure(PRES_100_ACCURACY);

        OLED_ClearArea(0, 16, 80, 32);
        sprintf(fmt_buf,"%d",PressuremmHgw);
        OLED_ShowString(16,16,fmt_buf,OLED_16X32);

        OLED_ClearArea(104, 48, 24, 16);
        sprintf(fmt_buf,"%d",g_sec);
        OLED_ShowString(104,48,fmt_buf,OLED_8X16);
        if (T125Flag == 1)
        {
            T125Flag = 0;
    		for(i=0;i<8;i++)
    		{
        		AverageCount[i]=AverageCount[i+1];
    		}
        	AverageCount[8]=PressureBackup;
        }
        if (T1Flag == 1)
        {
            T1Flag = 0;
            if (PressureBackup_last > PressureBackup)
            {
                //正在减小
                Mathlw = AverageCount[0] - AverageCount[7];
            }
            else
            {
                //正在增加
                Mathlw = 0;
            }
            if (Mathlw > 300)
                Math01b = 300;

            if (PressuremmHgw < 6)
            {
                OneSecDeflated = 0;
            }
            else
            {
                OneSecDeflated = Math01b;
            }
            #if (AKY_DEBUG_LCD_PRINT_ENABLE == 1)
            OLED_ClearArea(0, 56, 90, 8);
            sprintf(fmt_buf,"1s def=%d.%d",(OneSecDeflated/10),(OneSecDeflated%10));
            OLED_ShowString(0,56,fmt_buf,OLED_6X8);
            #endif
            PressureBackup_last = PressureBackup;//记住上一次压力值

            if (g_sec == AIR_TEST_TIME / 2)
            {
                //记住中间时候的压力
    			L1=PressureBackup;

                #if (AKY_DEBUG_LCD_PRINT_ENABLE == 1)
                OLED_ClearArea(0, 48, 24, 8);
                sprintf(fmt_buf,"%d",L1);
                OLED_ShowString(0,48,fmt_buf,OLED_6X8);
                #endif
            }
        }

        if (g_sec > AIR_TEST_TIME)
        {
            //记住结束时候的压力
        	L2=PressureBackup;					
            break;
        }
        if (PressuremmHgw <= 1)
        {
            showerror('3');
            if (get_any_key_pres() == 0)
            {
                goto AIR_TIGHT_EXIT;
            }
        }
        
        if (KEY_POWER == 0)
        {
            goto AIR_TIGHT_EXIT;
        }
        if (KEY_MEM == 0)
        {
            goto AIR_TIGHT_RESTART;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }
        if (get_cur_off_time_tick() > OFF_TIME_TICK_1S_COUNT * 240)
        {
            showerror('3');
            if (get_any_key_pres() == 0)
            {
                goto AIR_TIGHT_EXIT;
            }
        }
        OLED_Update();
    }


    OLED_Clear();
    OLED_ShowChinese(0, 0,E_NO_INVERSE, "气密结果：");
    //计算结果
 	if(L1>L2) 
    {
        temp = (L1-L2);
    }
    else
    {
        temp = 0;
    }
    sprintf(fmt_buf,"%d.%d mmHg/min",temp/10,temp%10);
    OLED_ShowString(0,16,fmt_buf,OLED_8X16);

    #if (AKY_DEBUG_LCD_PRINT_ENABLE == 1)
    sprintf(fmt_buf,"L1=%dL2=%d",L1,L2);
    OLED_ShowString(0,56,fmt_buf,OLED_6X8);
    #endif
    OLED_Update();

    VALVE_OFF;
    PUMP_OFF;
    off_time_tick_clr();
    while (1)
    {
        if (KEY_POWER == 0)
        {
            goto AIR_TIGHT_EXIT;
        }
        if (KEY_MEM == 0)
        {
            goto AIR_TIGHT_RESTART;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }
        if (aky_off_time_trig(OFF_TIME))
        {
            goto AIR_TIGHT_EXIT;
        }
        
    }

AIR_TIGHT_EXIT:
    AKY_ASM_NOP();
    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}


void inflat_deflat_time_test(void)		//
{
	uint16 PressureBackup_last = 0;
//	uint16	temp;
    uint16 step = 0;
    uint16 pres_last = 0;
    uint16 fil_cnt = 0;
    uint16 fil_cnt2 = 0;


    g_s_set = get_flash_set_data();

    LCD_BL_ON;

INFLAT_DEFLAT_RESTART:
    VALVE_OFF;
    PUMP_OFF;

    OLED_Clear();
    // OLED_ShowChinese(0, 0,E_NO_INVERSE, "当前对应压力值：");
    // OLED_ShowString(16,16,"0",OLED_16X32);
    // OLED_ShowString(80,32,"mmHg",OLED_8X16);
    OLED_Update();
    
	wait_all_key_release();
    OLED_Clear();

    zerotracing(0);
    if (IsPowerOn == 0)
    {
        goto INFLAT_DEFLAT_EXIT;
    }
    
    wait_all_key_release();

    VALVE_ON;
    PUMP_OFF;
    step = 0;
    fil_cnt = 0;
    fil_cnt2 = 0;
    inflat_sec = 0;
    deflat_sec = 0;

    PressuremmHgw = 0;
    OLED_Clear();
    OLED_ShowChinese(2, 0,E_NO_INVERSE, "实时");
    OLED_ShowChinese(2, 16,E_NO_INVERSE, "压力");
    sprintf(fmt_buf,"%d",PressuremmHgw);
    OLED_ShowString(40,0,fmt_buf,OLED_16X32);
    OLED_ShowString(96,16,"mmHg",OLED_8X16);
    OLED_DrawRectangle(0, 0, 128, 32,OLED_UNFILLED);
    OLED_DrawLine(35,0,35,32);

    OLED_ShowChinese(0, 32,E_NO_INVERSE, "限定值");
    sprintf(fmt_buf,":%d",g_s_set.inflat_limit_pres_val);
    OLED_ShowString(48,32,fmt_buf,OLED_8X16);
    OLED_ShowChinese(0, 48,E_NO_INVERSE, "泄");
    OLED_ShowChinese(64, 48,E_NO_INVERSE, "充");

    OLED_Update();
    off_time_tick_clr();
    while (1)
    {
        
        if (flag_adc_ok == 1)
        {
            flag_adc_ok = 0;
            calpressure(PRES_100_ACCURACY);

            if (step == 0)
            {
                step++;
                off_time_tick_clr();
                pres_last = PressuremmHgw;   
                fil_cnt = 0;
                fil_cnt2 = 0;
            }
            else if (step == 1)
            {
                if (pres_last != PressuremmHgw)
                {
                    if (pres_last < PressuremmHgw)
                    {
                        //压力向上增长
                        fil_cnt++;
                        fil_cnt2 = 0;
                        if (fil_cnt > 5)
                        {
                            fil_cnt = 0;
                            step = 10;
                        }
                    }
                    else if (pres_last > PressuremmHgw)
                    {
                        //压力向下减少
                        fil_cnt = 0;
                        fil_cnt2++;
                        if (fil_cnt2 > 5)
                        {
                            fil_cnt2 = 0;
                            step = 20;
                        }
                    }
                }
                else
                {
                    fil_cnt = 0;
                    fil_cnt2 = 0;
                }
                if (aky_off_time_trig(OFF_TIME))
                {
                    goto INFLAT_DEFLAT_EXIT;
                }
            }
            else if (step == 10)
            {
                step++;
                inflat_sec = 0;    
                T1Flag_time_cnt = 0;         
            }
            else if (step == 11)
            {
                if (PressuremmHgw >= g_s_set.inflat_limit_pres_val)
                {
                    PUMP_OFF;
                    step++;
                } 
                if (pres_last != PressuremmHgw)
                {
                    if (pres_last > PressuremmHgw)
                    {
                        //压力向下减少
                        fil_cnt = 0;
                        fil_cnt2++;
                        if (fil_cnt2 > 5)
                        {
                            fil_cnt2 = 0;
                            step = 20;
                        }
                    }
                    else
                    {
                        fil_cnt2 = 0;
                    }
                }

                if (T1Flag)
                {
                    T1Flag = 0;
                    inflat_sec++;
                }        
            }
            else if (step == 12)
            {
                if (pres_last != PressuremmHgw)
                {
                    if (pres_last > PressuremmHgw)
                    {
                        //压力向下减少
                        fil_cnt = 0;
                        fil_cnt2++;
                        if (fil_cnt2 > 5)
                        {
                            fil_cnt2 = 0;
                            step = 20;
                        }
                    }
                    else
                    {
                        fil_cnt2 = 0;
                    }
                }      
            }

            else if (step == 20)
            {
                step++;
                deflat_sec = 0;  
                T1Flag_time_cnt = 0;         

            }
            else if (step == 21)
            {
                if (PressuremmHgw <= 1)
                {
                    PUMP_OFF;
                    if (deflat_sec <= 0)
                    {
                        deflat_sec = 1;
                    }
                    step++;
                } 
                if (pres_last != PressuremmHgw)
                {
                    if (pres_last < PressuremmHgw)
                    {
                        //压力向上增长
                        fil_cnt++;
                        fil_cnt2 = 0;
                        if (fil_cnt > 5)
                        {
                            fil_cnt = 0;
                            if (deflat_sec <= 0)
                            {
                                deflat_sec = 1;
                            }
                            
                            step = 10;
                        }
                    }
                    else
                    {
                        fil_cnt = 0;
                    }
                }
                
                if (T1Flag)
                {
                    T1Flag = 0;
                    deflat_sec++;
                }       
            }
            else if (step == 22)
            {
                if (pres_last != PressuremmHgw)
                {
                    fil_cnt = 0;
                }
                else 
                {
                    //等待稳定
                    fil_cnt++;
                    if (fil_cnt > 10)
                    {
                        fil_cnt = 0;
                        step = 0;
                    }
                }   
            }
            pres_last =  PressuremmHgw;

            if (deflat_sec > 600)
            {
                showerror('3');
                if (get_any_key_pres() == 0)
                {
                    goto INFLAT_DEFLAT_EXIT;
                }
            }
            if (inflat_sec > 600)
            {
                showerror('3');
                if (get_any_key_pres() == 0)
                {
                    goto INFLAT_DEFLAT_EXIT;
                }
            }
            if (PressuremmHgw > MAX_PRESURE)
            {
                showerror('7');
                if (get_any_key_pres() == 0)
                {
                    goto INFLAT_DEFLAT_EXIT;
                }
            }
        }
        if (T125Flag == 1)
        {
            T125Flag = 0;
            OLED_ClearArea(40,0,48,32);
            sprintf(fmt_buf,"%d",PressuremmHgw);
            OLED_ShowString(40,0,fmt_buf,OLED_16X32);
            OLED_DrawRectangle(0, 0, 128, 32,OLED_UNFILLED);

            OLED_ClearArea(16,48,48,16);
            sprintf(fmt_buf,":%ds",deflat_sec);
            OLED_ShowString(16,48,fmt_buf,OLED_8X16);

            OLED_ClearArea(80,48,48,16);
            sprintf(fmt_buf,":%ds",inflat_sec);
            OLED_ShowString(80,48,fmt_buf,OLED_8X16);
            OLED_Update();
        }
        

        if (KEY_POWER == 0)
        {
            goto INFLAT_DEFLAT_EXIT;
        }
        if (KEY_MEM == 0)
        {
            goto INFLAT_DEFLAT_RESTART;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }

    }
INFLAT_DEFLAT_EXIT:
    AKY_ASM_NOP();
    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}

// //生产用快速测试界面
void fast_test_mode(void)
{
    uint16_t i;
    uint8_t flag_low_bat = 0;
    uint8_t flag_pump_val_on = 0;
    LCD_BL_ON;

    OLED_Clear();
    OLED_ShowChinese(0, 0,E_NO_INVERSE, "快速测试模式");
    OLED_Update();
    aky_tick_delay_ms(1000);
    OLED_Clear();
    OLED_DrawRectangle(0,0,128,64,OLED_FILLED);
    OLED_Update();
    aky_tick_delay_ms(3000);
    wait_all_key_release();
    OLED_Clear();
    OLED_ShowChinese(0, 0,E_NO_INVERSE, "快速测试模式");
    OLED_Update();

    off_time_tick_clr();
    check_low_bat_init();
    while (1)
    {
        if ((get_cur_off_time_tick() % OFF_TIME_TICK_100MS_COUNT) == (OFF_TIME_TICK_10MS_COUNT * 3))
        {
            if (flag_pump_val_on == 0)
            {
                if (check_low_bat_trig(LOW_BAT_VAL - 10) == 1)
                {
                    flag_low_bat = 1;
                }
            }
        }

        if (flag_low_bat == 1)
        {
            PUMP_OFF;
            VALVE_OFF;
            for (i = 0; i < 5; i++)
            {
                LCD_BL_ON;
                OLED_Clear();
                OLED_ShowChinese(16, 0, E_NO_INVERSE,"电池电量不足");
                low_bat_icon_on();
                OLED_Update();
                beep_times(1);
                if (KEY_POWER == 0)
                {
                    goto FAST_TEST_EXIT;                
                }
                
                if (check_low_bat_trig(LOW_BAT_VAL - 10) == 0)
                {
                    OLED_Clear();
                    OLED_ShowChinese(0, 0,E_NO_INVERSE, "快速测试模式");
                    flag_low_bat = 0;
                }
            }
        }
        else
        {
            if ((get_cur_off_time_tick() % OFF_TIME_TICK_1S_COUNT) < OFF_TIME_TICK_100MS_COUNT * 5)
            {
                flag_pump_val_on = 1;
                PUMP_ON;
                VALVE_ON;
            }
            else
            {
                flag_pump_val_on = 0;
                PUMP_OFF;
                VALVE_OFF;
            }
        }
        if (get_cur_off_time_tick() % (OFF_TIME_TICK_1S_COUNT * 3) == OFF_TIME_TICK_1S_COUNT * 1)
        {
            LCD_BL_OFF;
            OLED_ClearArea(0,16,16,16);
            OLED_ShowNum(0,16,1,1,OLED_8X16);
        }
        else if (get_cur_off_time_tick() % (OFF_TIME_TICK_1S_COUNT * 3) == OFF_TIME_TICK_1S_COUNT * 2)
        {
            LCD_BL_ON;
            OLED_ClearArea(0,16,16,16);
            OLED_ShowNum(0,16,2,1,OLED_8X16);
        }
        else if (get_cur_off_time_tick() % (OFF_TIME_TICK_1S_COUNT * 3) == (OFF_TIME_TICK_1S_COUNT * 3) - 1)
        {
            OLED_ClearArea(0,16,16,16);
            OLED_ShowNum(0,16,3,1,OLED_8X16);
        }
        OLED_Update();


        if (KEY_POWER == 0)
        {
            break;
        }
    }
FAST_TEST_EXIT:
    VALVE_OFF;
    PUMP_OFF;
    OLED_Clear();
    OLED_ShowChinese(0, 16,E_NO_INVERSE, "退出");

    OLED_Update();
    long_beep();
    aky_tick_delay_ms(1000);
    PUMP_OFF;
    VALVE_OFF;
    wait_all_key_release();

    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}

void pres_meter_mode(void)		//
{

    LCD_BL_ON;

//PRES_METER_RESTART:
    VALVE_OFF;
    PUMP_OFF;

    OLED_Clear();
    OLED_Update();
    
	wait_all_key_release();
    OLED_Clear();

    zerotracing(0);
    if (IsPowerOn == 0)
    {
        goto PRES_METER_EXIT;
    }
    
    wait_all_key_release();


    OLED_Clear();
	OLED_ShowChinese(16, 0,E_NO_INVERSE, "实时压力值：");
    sprintf(fmt_buf, "%03d:%d0", PressuremmHgw / 10,PressuremmHgw%10);
    OLED_ShowString(16,16,fmt_buf,OLED_16X32);
    OLED_ShowString(88,48,"mmHg",OLED_8X16);
    olde_fmt_num(0, 56,OLED_6X8,"",SensorCountw);

    OLED_Update();
    aky_tick_delay_ms(300);
    VALVE_ON;
    PUMP_OFF;
    off_time_tick_clr();
    while (1)
    {
        if (flag_adc_ok == 1)
        {
            flag_adc_ok = 0;
            calpressure(PRES_1000_ACCURACY);
        }
        if (T125Flag == 1)
        {
            T125Flag = 0;
            OLED_ClearArea(16,16,96,32);
            sprintf(fmt_buf, "%03d:%d0", PressuremmHgw / 10,PressuremmHgw%10);
            OLED_ShowString(16,16,fmt_buf,OLED_16X32);

            OLED_ClearArea(0,56,36,8);
            olde_fmt_num(0, 56,OLED_6X8,"",SensorCountw);
            OLED_Update();
        }
        

        if (KEY_POWER == 0)
        {
            goto PRES_METER_EXIT;
        }
        if (KEY_MEM == 0)
        {
            if ((PressuremmHgw / 10) < MAX_PRESURE)
            {
                PUMP_ON;
            }
            else
            {
                PUMP_OFF;
            }
        }
        else
        {
            PUMP_OFF;
        }
        if (KEY_SET == 0)
        {
            IsPowerOn = 1;
            g_e_sys_stat = MENU_SEL_MODE;
            return;
        }

        if ((PressuremmHgw / 10) > 1)
        {
            off_time_tick_clr();
        }
        
        if (aky_off_time_trig(OFF_TIME))
        {
            goto PRES_METER_EXIT;
        }
    }

PRES_METER_EXIT:
    AKY_ASM_NOP();
    IsPowerOn = 0;
    g_e_sys_stat = STOP_MODE;
}
